package com.zzgc.modules.controller;

import com.zzgc.common.entity.AuthDepart;
import com.zzgc.common.entity.AuthUser;
import com.zzgc.common.entity.School;
import com.zzgc.common.response.HttpStatus;
import com.zzgc.common.response.Result;
import com.zzgc.common.utils.StringUtils;
import com.zzgc.modules.log.ApiLog;
import com.zzgc.modules.param.AuthUserSchoolParam;
import com.zzgc.modules.service.AuthDepartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * 学校管理控制类
 * @Author:
 * @CreateTime: 2023-05-08  11:10
 * @Description:
 * @Version: 1.0
 */
@RestController
@RequestMapping("school")
@ApiLog(value = "学校管理控制类", url = "school")
@Api(tags = "学校管理")
public class SchoolController extends BaseController{

    @GetMapping("get/list")
    @ApiLog(value = "获取学校列表展示", url = "get/list", method = RequestMethod.GET)
    @ApiOperation(value = "获取学校列表展示", notes = "获取学校列表展示")
    public Result getSchoolList(String dis,String city,String schoolName, String schoolType, Integer size, Integer page,String district) {
        String userRole = super.getUserRole();
        //判断是否是河南省教育局
        if (!userRole.equals("1") && !userRole.equals("2")) {
            //县级用户
            if (!userRole.equals("3")) {
                district =super.getDepartId();
            }else {
                //市级用户
                List<Integer> departIds = departService.getAllDepart(super.getDepartId());
                dis = departIds.toString().replace("[", "(").replace("]", ")");
            }
        }
        if (!userRole.equals("1") && !userRole.equals("2") && !userRole.equals("3") && !userRole.equals("4")) {
            return Result.builder().code(500).msg("用户无权限").build();
        }
        if (StringUtils.isNull(size)) {
            size = 10;
        }
        if (StringUtils.isNull(page)) {
            page = 1;
        }
        List<School> schools = schoolService.queryByLimit(dis,city,district,schoolName,schoolType,size,page);

        HashMap<String, Object> map = new HashMap<>();
        map.put("total", schools.size() == 0 ? 0 : schools.get(0).getTotal());
        map.put("data", schools);
        return Result.builder().code(HttpStatus.OK.value())
                .msg(HttpStatus.OK.getReasonPhrase()).data(map).build();
    }

    @PostMapping("add")
    @ApiLog(value = "添加学校信息", url = "add", method = RequestMethod.POST)
    @ApiOperation(value = "添加学校信息", notes = "添加学校信息")
    public Result addSchool(@Validated @RequestBody AuthUserSchoolParam authUser) {
        // 判断是否是省级市级教育局, 只有省级市级教育局才能添加学校
        String userRole = super.getUserRole();
        if (!userRole.equals("1") && !userRole.equals("2") && !userRole.equals("3")) {
            return Result.builder().code(500).msg("权限不足!").build();
        }
        // 校验手机号和账号, 校验重复值
        List<AuthUser> authUsers = userService.queryAllByNamePwd(authUser.getAccount(), authUser.getMobile());
        if (authUsers.size() > 0) { // 用户名已存在
            return Result.builder().code(500).msg("用户名或手机号已占用!").build();
        }
        // 校验部门信息(学校名称)是否重复
        AuthDepart authDepart = new AuthDepart();
        authDepart.setAName(authUser.getSchoolName());
        long count = departService.count(authDepart);
        if (count > 0) {
            return Result.builder().code(500).msg("已存在该学校,添加失败!").build();
        }

        // 创建用户, 此方法创建学校角色用户, 学校和部门的数据表
        AuthUserSchoolParam insert = userService.insertUser(authUser);

        if (insert == null) {
            return Result.builder().code(HttpStatus.FAILED_OPERATION.value())
                    .msg(HttpStatus.FAILED_OPERATION.getReasonPhrase()).build();
        }
        return Result.builder().code(HttpStatus.OK.value())
                .msg(HttpStatus.OK.getReasonPhrase()).data(authUser).build();
    }

}
